package com.lanou.userLogin.order.web.servlet;

import com.lanou.userLogin.book.domain.Book;
import com.lanou.userLogin.book.service.BookService;
import com.lanou.userLogin.order.domain.Order;
import com.lanou.userLogin.order.domain.OrderItem;
import com.lanou.userLogin.order.service.OrderService;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.List;

@WebServlet("/myOrderServlet")
public class MyOrderServlet extends HttpServlet {

    private OrderService os = new OrderService();
    private BookService bs = new BookService();

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //根据uid从数据库获取的订单信息
        HttpSession session = req.getSession();

        String uid = (String) session.getAttribute("uid");

        List<Order> orderList = os.selectOrder(uid);
        //System.out.println(orderList);
        //System.out.println(orderList.size()+"-----------------------");

        for (Order order : orderList) {
            // 通过id获取每个订单下的订单项集合
            List<OrderItem> orderItemList = os.selectOrderItem(order.getOid());
            //System.out.println(orderItemList);

            for (OrderItem orderItem : orderItemList) {
                Book book = bs.oneBook(orderItem.getBid());
                //System.out.println(book);
                orderItem.setBook(book);
                //System.out.println(orderItem);
            }
            order.setOrderItemList(orderItemList);
            //System.out.println(order);
        }
        //System.out.println(orderList);
        req.setAttribute("orderList", orderList);
        req.getRequestDispatcher("/jsps/order/list.jsp").forward(req, resp);
    }
}
